﻿using Common.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Video.Helper
{
    /// <summary>
    /// 操作上下文
    /// </summary>
    public class OperateContext
    {
        const string Admin_CookiePath = "/admin/";
        const string Admin_InfoKey = "ainfo";
        const string Admin_PermissionKey = "apermission";
        const string Admin_TreeString = "aTreeString";
        const string Admin_LogicSessionKey = "BLLSession";

        #region 0.1 Http上下文 及 相关属性
        /// <summary>
        /// Http上下文
        /// </summary>
        HttpContext ContextHttp
        {
            get
            {
                return HttpContext.Current;
            }
        }

        HttpResponse Response
        {
            get
            {
                return ContextHttp.Response;
            }
        }

        HttpRequest Request
        {
            get
            {
                return ContextHttp.Request;
            }
        }

        System.Web.SessionState.HttpSessionState Session
        {
            get
            {
                return ContextHttp.Session;
            }
        }
        #endregion

        #region 0.2实例构造函数 初始化 业务仓储
        public OperateContext()
        {
            BLLSession = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");
        }
        #endregion

        #region 0.3 业务仓储 +IBLL.IBLLSession BLLSession
        /// <summary>
        /// 业务仓储
        /// </summary>
        public IBLL.IBLLSession BLLSession;
        #endregion

        #region 0.4 获取当前 操作上下文 + OperateContext Current
        /// <summary>
        /// 获取当前 操作上下文 (为每个处理浏览器请求的服务器线程 单独创建 操作上下文)
        /// </summary>
        public static OperateContext Current
        {
            get
            {
                OperateContext oContext = CallContext.GetData(typeof(OperateContext).Name) as OperateContext;
                if (oContext == null)
                {
                    oContext = new OperateContext();
                    CallContext.SetData(typeof(OperateContext).Name, oContext);
                }
                return oContext;
            }
        }
        #endregion

        ////-----------------------------1.0持久化对象--------------------------------

        #region 当前用户对象
        public MODEL.user UserSession
        {
            get
            {
                return Session[Admin_InfoKey] as MODEL.user;
            }
            set
            {
                Session[Admin_InfoKey] = value;
            }
        }
        #endregion

        ////---------------------------------------------2.0 用户处理--------------------

        #region 用户注册  +RegistResult(string eName, string Email, string Pwd)
        public bool RegistResult(string eName, string Email, string Pwd)
        {
            try
            {
                MODEL.user RegistUser = new MODEL.user()
                {
                    user_ID=System.Guid.NewGuid(),
                    user_eName = eName,
                    user_Email = Email,
                    user_Pwd = Pwd,
                    user_RegistDate=DateTime.Now
                };
                if (OperateContext.Current.BLLSession.IuserBLL.Add(RegistUser) > 0) 
                {
                    return true;
                }
            }
            catch (Exception ex)
            {

            }
            return false;
        } 
        #endregion

        #region 用户登录  +LoginInfoCheck(string Email, string Pwd, string IsAlway)
        public MODEL.user LoginInfoCheck(string Email, string Pwd,string IsAlway)
        {
            try
            {
                //把对象保存在session
                UserSession = OperateContext.Current.BLLSession.IuserBLL.GetListBy(p => p.user_Email == Email && p.user_Pwd == Pwd).FirstOrDefault();
                if (IsAlway == "yes")
                {
                    string strCookieValue = Common.SecurityHelper.EncryptUserInfo(UserSession.user_ID.ToString());
                    //2.1.3创建cookie
                    HttpCookie cookie = new HttpCookie(Admin_InfoKey, strCookieValue);
                    cookie.Expires = DateTime.Now.AddDays(7);
                    cookie.Path = Admin_CookiePath;
                    Response.Cookies.Add(cookie);
                }
                return UserSession;
            }
            catch (Exception ex)
            {

            }
            return null;
        } 
        #endregion

        #region  判断当前用户是否登陆 +bool IsLogin()
        /// <summary>
        /// 判断当前用户是否登陆 而且
        /// </summary>
        /// <returns>model.user</returns>
        public MODEL.user IsLogin()
        {
            string user_ID;
            //1.验证用户是否登陆session
            if (Session[Admin_InfoKey] == null)
            {
                //验证cookie
                if (Request.Cookies[Admin_InfoKey] == null)
                {
                    return null;
                }
                else
                {
                    user_ID = Common.SecurityHelper.DecryptUserInfo(Request.Cookies[Admin_InfoKey].ToString());
                    //有记住密码就重cookie中获取用户ID
                    Session[Admin_InfoKey] = OperateContext.Current.BLLSession.IuserBLL.GetListBy(p => p.user_ID.Equals(user_ID));
                }
            }
            return Session[Admin_InfoKey] as MODEL.user;
        }
        #endregion


        ////---------------------------------------------3.0 公用操作方法--------------------

        #region 3.1 生成 Json 格式的返回值 +ActionResult RedirectAjax(string statu, string msg, object data, string backurl)
        /// <summary>
        /// 生成 Json 格式的返回值
        /// </summary>
        /// <param name="statu"></param>
        /// <param name="msg"></param>
        /// <param name="data"></param>
        /// <param name="backurl"></param>
        /// <returns></returns>
        public JsonResult RedirectAjax(string statu, string msg, object data, string backurl)
        {
            MODEL.FormatModel.AjaxMsgModel ajax = new MODEL.FormatModel.AjaxMsgModel()
            {
                Statu = statu,
                Msg = msg,
                Data = data,
                BackUrl = backurl
            };
            JsonResult res = new JsonResult();
            res.Data = ajax;
            res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return res;
        }
        #endregion


    }
}
